var flag = false;


function exchange(id){
	flag = false;
	//判断用户是否登录
	if(userId == 0){
		showLoginForm();
	}else{
		productId = id;
		addProductToCard(false);
	}
}

function addCart(id){
	flag = true;
	//判断用户是否登录
	if(userId == 0){
		showLoginForm();
	}else{
		productId = id;
		addProductToCard(false);
	}
}

//repeat是否可以重复
function addProductToCard(repeat){
	
	var urlPath = path + "/cart/add/product.htm?repeat="+repeat+"&flag="+flag+"&userId="+userId+"&productId="+productId;
	
	$.ajax({
		   type: "POST",
		   url: urlPath,
		   data: $("#product-from").serialize(),
		   success: evalCallback
		});
}

function evalCallback(result){
	eval(result);
}

function showLoginForm()
{
	//获取窗口大小
	var $width = $(window).width();
	var $height = $(window).height();
	
	//获取弹出窗口的大小
	var $popWidth = $(".pop-login").width();
	var $popHeight = $(".pop-login").height();
	
	//重新定位弹出窗口
	var $offsetLeft = ($width - $popWidth)/2;
	var $offsetTop = ($height - $popHeight)/2;
	
	var $mark = $('<div class="mask-layer"></div>');
	var $popLogin = $(".pop-login");
//	$("#miniNavLogAct").click(function(){
	$popLogin.show(400);
	$popLogin.css({"left":$offsetLeft,"top":$offsetTop});
	//在body处插入刚刚创建的遮罩层
	$('body').prepend($mark);
	var $bodyHeight = $("body").height();
	$mark.css({"width":"100%","height":$bodyHeight});
	$mark.show();
	$popLogin.visible = false;
//		return false;
//	});
	//关闭操作
	if("$popLogin:visible"){
		$("#pop-colse").click(function(){
			$popLogin.hide(400);
			$mark.hide();
			return false;
		});
	};
}

function login(){
	var username = $('#username').attr('value');
	var password = $('#password').attr('value');
	var vaildcode = $('#vaildcode').attr('value');
	if(username == "" || username == null || username == undefined){
		$('#errorMsg').html("帐号不能为空！");
		return;
	}
	
	if(password == "" || password == null || password == undefined){
		$('#errorMsg').html("密码不能为空！");
		return;
	}
	
	if(vaildcode == "" || vaildcode == null || vaildcode == undefined){
		$('#errorMsg').html("验证码不能为空！");
		return;
	}
	
	password = hex_md5(password);
	
	$.ajax({
		   type: "POST",
		   url: path + "/product/login.htm?username=" + username + "&password=" + password + "&vaildcode=" + vaildcode,
		   success: loginCallback
		});
}

function loginCallback(result){
	if(result=="0"){
		$('#errorMsg').html("帐号、密码不能为空！");
	}else if(result=="1"){
		$('#errorMsg').html("验证码不正确！");
	}else if(result=="2"){
		$('#errorMsg').html("用户和密码不存在！");
	}else{
		if(detailIds == null || detailIds == ""){
			addProductToCard(false);
		}else{
			if(hasAdd(productId)){
				addProductToCard(false);
			}else{
				var submit = function (v, h, f) {
			        if (v == 'ok') {
			        	addProductToCard(true);
			        }
			        else if (v == 'cancel') {
			        	location.reload();
			        }
			        return true; //close
			    };
			    $.jBox.confirm("购物车中已有是否继续？", "提示", submit);
			}
		}
	}
}

function removeCart(id){
	var str = '$("#dl_'+id+'").remove();';
	eval(str);
	
	var $car = $("#J-mini-cart-full dl");
	if($car.length > 0){
		var $del = $("#J-mini-cart-full .del");
		$("#J-mini-cart-empty").hide();
	}else {
		$("#J-mini-cart-empty").show();
		$("#J-mini-cart-full").hide();
	}
	
	var urlPath = path + "/cart/del.htm?detailId="+id+"&userId="+userId;
	$.ajax({
	   type: "POST",
	   url: urlPath,
	   success: evalCallback
	});
}